<template>
  <div>
    <!-- 表格 -->
    <el-table :data="records" style="width: 100%" border>
      <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称"></el-table-column>
      <el-table-column prop="date" label="描述"></el-table-column>
      <el-table-column prop="skuDefaultImg" label="默认图片" width="80px">
        <template slot-scope="scope">
          <img :src="scope.row.skuDefaultImg" alt="" width="50px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量（KG）" width="80px"></el-table-column>
      <el-table-column prop="price" label="价格（元）" width="80px"></el-table-column>
      <el-table-column prop="date" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" icon="el-icon-bottom" type="success" v-if="scope.row.isSale==0"
            @click="sale(scope.row)"></el-button>
          <el-button size="mini" icon="el-icon-top" type="info" v-else @click="cancel(scope.row)">
          </el-button>
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="edit"></el-button>
          <el-button size="mini" icon="el-icon-info" type="info" @click="getSkuInfo(scope.row)">
          </el-button>
          <el-button size="mini" icon="el-icon-delete" type="danger" @click="delSku(scope.row)">
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination style="margin-top: 20px; text-align: center;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="page" :page-sizes="[3, 5,10]"
      :page-size="limit" layout="prev, pager, next, jumper, ->,sizes,total" :total="total">
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag type="success" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id"
            style="margin-right: 10px;">{{ attr.attrId }}-{{ attr.valueId }}</el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel>
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>

  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      // skuList:[]
      records: [],
      skuInfo: {}, //存储sku信息
      drawer: false,
    };
  },
  mounted() {
    this.skuList();
  },
  methods: {
    skuList() {
      this.$API.sku.reqSkuList1(this.page, this.limit).then((res) => {
        console.log(res);
        this.total = res.data.total;
        this.records = res.data.records;
      });
    },
    handleSizeChange(val) {
      this.limit = val;
      this.skuList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.skuList();
    },
    //上架按钮的回调函数,下箭头为上架按钮
    sale(row) {
      // console.log(row);
      this.$API.sku.reqSale(row.id).then((res) => {
        console.log(res);
        if (res.code == 200) {
          // this.skuList(this.page);
          row.isSale = 1;
          this.$message({ type: 'success', message: '上架成功' });
        }
      });
    },
    // 下架按钮的回调函数
    cancel(row) {
      this.$API.sku.reqCancel(row.id).then((res) => {
        if (res.code == 200) {
          row.isSale = 0;
          this.$message({ type: 'success', message: '下架成功' });
        }
      });
    },
    edit() {
      this.$message({ type: 'info', message: '正在开发中' });
    },
    // 获取sku详情的方法
    getSkuInfo(sku) {
      // 展示抽屉
      this.drawer = true;
      this.$API.sku.reqSkuById(sku.id).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.skuInfo = res.data;
        }
      });
    },
    delSku(sku) {
      this.$API.sku.reqDel(sku.id).then((res) => {
        this.$confirm('此操作将永久删除该sku, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
              this.skuList();
            } else {
              this.$message.error('删除失败');
            }
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      });
    },
  },
};
</script>

<style scoped>
.el-button {
  border: 0;
}
.el-row .el-col-5 {
  font-size: 20px;
  text-align: right;
  font-weight: 500;
}
.el-col {
  margin: 10px 10px;
}
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>